<template>
  <a-spin :spinning="uploading" tip="上传中...">
    <div class="uplaodBoxB page-miH page-bgBack">
      <a-row class="head">
        <div style="cursor: pointer" @click="isGoBack">
          <a-icon type="left" />
          <div class="left2">返回</div>
        </div>
        <a-col class="title">
          <strong>导入回款记录</strong>
        </a-col>
      </a-row>
      <a-spin :spinning="spinning" tip="加载中...">
        <div class="ant-upload-select-text-box" style="padding-top: 24px" v-if="!callback">
          <a-upload ref="upload" :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload">
            <img :src="uploadImg" style="width: 139px; height: 106px" />
            <div class="ant-modal-body">
              <div style="font-size: 12px; color: #666">点击添加文件或将文件拖拽到此处</div>
              <div style="font-size: 12px; color: #999">(文件大小不超过5M)</div>
            </div>
            <a-button style="margin-bottom: 15px" type="primary"> <a-icon type="plus" />添加文件 </a-button>
          </a-upload>
        </div>
        <div class="tipBox" v-if="!callback">
          <a-row style="color: #252525; font-size: 16px; font-weight: 500">温馨提示</a-row>
          <a-row>
            <a-col :span="12">
              <!-- <span style="padding-right: 5px">
              首先下载标准模板，填写信息后再上传
            </span>
              <a-button type="link" @click="download" class="primary_color" style="font-size: 12px">点击下载模板</a-button>-->
              <!-- <div>1.支持WPS、excel2003以上版本文件</div> -->
              <div style="margin-top: 12px" class="rowMargin">
                请先下载模板，根据模板填写信息后再进行上传
                <span class="button" @click="download">点击下载模板</span>
              </div>
              <div class="rowMargin">1.该功能用于进行导入回款记录</div>
              <div class="rowMargin">2.模板文件大小不得超过5M，单次导入上传数据不得超过1000条</div>
              <div class="rowMargin">3.支持wps，excel2003以上版本文件，支持xlsx格式</div>
              <div class="rowMargin">4.请不要上传加密或带宏的文件，不然将导致上传错误；</div>
            </a-col>
          </a-row>
        </div>
      </a-spin>
      <!-- <employee-select
        :show.sync="isChooseStaff"
        :default-selected-keys="selectedList"
        :selected-list.sync="selectedList"
        :status="3"
        @change="onEmployeeSelectChange"
      /> -->
      <div v-if="callback">
        <div style="padding-left: 24px; padding-right: 24px">
          <div
            style="
              margin-top: 15px;
              padding-left: 10px;
              display: flex;
              flex-flow: row;
              align-items: center;
              height: 35px;
              width: 100%;
              background-color: #fffbe6;
              border-style: solid;
              border-color: #ffe58f;
              border-width: 1px;
              border-radius: 5px;
            "
          >
            <img style="width: 16px; height: 16px; margin-right: 5px" src="../../../assets/icons/重复.png" />
            <span sytle="color:#55565D;font-size:14px;">
              共{{ count }}条数据，成功导入{{ succ }}条数据， <label class="redc">{{ fial }}</label
              >条数据导入失败；异常数据无法导入。
            </span>
          </div>
        </div>
        <div class="header-title-box">
          <div class="headerTitle">
            <div class="block" />
            <div style="font-size: 15px; font-weight: 700; font-color: #252525">错误信息</div>
          </div>
          <div class="btn-box">
            <a-button type="primary" @click="buCha">导出错误数据</a-button>
          </div>
        </div>
        <a-table
          style="padding-left: 24px; padding-right: 24px"
          :columns="columnDetail"
          :pagination="pagination"
          :data-source="dataDetail"
          :scroll="{ x: 800 }"
          @change="handleTableChange"
        >
          <!-- <template slot="reason" slot-scope="text">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ text }}</span>
              </template>
              <span style="color:#ff0000">{{ text }}</span>
            </a-tooltip>
          </template> -->
          <template slot="name" slot-scope="text, record">
            <a-popover placement="top" v-if="record.errorInfo.name">
              <template slot="content">
                <p>{{ record.errorInfo.name }}</p>
              </template>
              <template slot="title">
                <span>错误信息</span>
              </template>
              <span style="color: #ff0000">{{ text }}</span>
            </a-popover>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="idCard" slot-scope="text, record">
            <a-popover placement="top" v-if="record.errorInfo.idCard">
              <template slot="content">
                <p>{{ record.errorInfo.idCard }}</p>
              </template>
              <template slot="title">
                <span>错误信息</span>
              </template>
              <span style="color: #ff0000">{{ text }}</span>
            </a-popover>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="amount" slot-scope="text, record">
            <a-popover placement="top" v-if="record.errorInfo.amount">
              <template slot="content">
                <p>{{ record.errorInfo.amount }}</p>
              </template>
              <template slot="title">
                <span>错误信息</span>
              </template>
              <span style="color: #ff0000">{{ text }}</span>
            </a-popover>
            <span v-else>{{ text }}</span>
          </template>
          <template slot="wayStatus" slot-scope="text, record">
            <a-popover placement="top" v-if="record.errorInfo.wayStatus">
              <template slot="content">
                <p>{{ record.errorInfo.wayStatus }}</p>
              </template>
              <template slot="title">
                <span>错误信息</span>
              </template>
              <span style="color: #ff0000">{{ ["银行", "微信", "支付宝", "其他", "余额扣款", "--"][text ? text - 1 : 5] }}</span>
            </a-popover>

            <span v-else>{{ ["银行", "微信", "支付宝", "其他", "余额扣款"][text - 1] }}</span>
          </template>
        </a-table>
      </div>

      <a-modal v-model="dialogFailVisible" title="批量导入" width="500px" @ok="handleFailClose">
        <template slot="footer">
          <a-button key="submit" type="primary" @click="handleFailClose">确定</a-button>
        </template>
        <div style="padding-left: 10px; display: flex; flex-flow: row; align-items: center; height: 35px; width: 100%">
          <img style="width: 25px; height: 25px; margin-right: 10px" src="../../../assets/icons/duihao.png" />
          <div style="color: #3c3d43; font-size: 15px; font-weight: 700">共{{ count }}条数据，全部导入成功！</div>
        </div>
      </a-modal>
    </div>
  </a-spin>
</template>
<script>
import UploadMixin from "xqjr-plugin-common/UploadOrExport/uploadMixin";
import moment from "moment";
const mixin = new UploadMixin();
const formalType = ["试用", "正式", "实习"];
const columnDetail = [
  {
    title: "错误信息",
    dataIndex: "reason",
    fixed: "left",
    scopedSlots: { customRender: "reason" },
    ellipsis: true,
    width: 260
  }
];
import axios from "axios";
import qs from "qs";
export default {
  components: {},
  mixins: [mixin],
  data() {
    return {
      spinning: false,
      count: 0,
      selectedList: [],
      succ: 0,
      fial: 0,
      errData: {},
      formalType,
      isChooseStaff: false,
      pagination: {
        pageSize: 10,
        total: 0,
        current: 1,
        showQuickJumper: true
      },
      dataDetail: [],
      callback: false,
      dialogFailVisible: false,
      downloadurl: undefined,
      columnDetail,
      uploadImg: require("@/assets/img/uploadImg.png"),
      params: {
        templateName: "socBackImportTemplate.xlsx"
      },
      url: `/hr/api/v1/excel/template/download`,
      excelName: "导入回款记录模板.xlsx"
    };
  },

  created() {
    const companyId = this.$store.state.hr.companyId;
    this.action = `/hr/api/v1/empSocBackRecordRecords/importRecord`;
  },
  methods: {
    beforeUpload(file) {
      if (this.fileList.length > 0) {
        this.$message("最多只能上传一个");
        return false;
      }
      this.fileList = [...this.fileList, file];
      if (this.fileList && this.fileList.length > 0) {
        const { fileList } = this;
        const formData = new FormData();
        fileList.forEach((file) => {
          formData.append("file", file);
          formData.append("month", this.$route.query.month);
          formData.append("year", this.$route.query.year);
          formData.append("companyIds", this.$route.query.companyIds);
        });
        this.uploading = true;

        axios({
          url: this.action,
          method: "post",
          data: formData,
          headers: this.headers
        }).then((res) => {
          this.reset();
          this.handleSuccess(res.data);
          this.uploading = false;
        });
      } else {
        this.reset();
        this.handleUploadError();
      }
      return false;
    },
    isGoBack() {
      // if (this.callback) {
      //   this.callback = false;
      // } else {
      this.goback();
      // }
    },
    handleSuccess(res) {
      this.errData = res.data;
      var that = this;
      if (res.success && res.data !== null) {
        var numAll = res.data.allCount;
        var numFail = res.data.errorCount;
        const numSuc = res.data.successCount;
        if (that.showFaild) {
          that.Faildata = res.data.errorList;
          that.Faildata.forEach((item, index) => {
            item = Object.assign(item, item.allMap);
          });
          that.showFaild(numAll, numSuc, numFail, that.Faildata);
        } else {
          if (numFail === 0) {
            this.$confirm({
              okText: "确定",
              title: `共修改${numAll}人，${numSuc}人成功，${numFail}人未成功`,
              onOk() {
                that.goback();
              },
              onCancel() {
                that.goback();
              }
            });
          } else {
            this.$confirm({
              okText: "确定",
              cancelText: "查看未成功名单",
              title: `共修改${numAll}人，${numSuc}人成功，${numFail}人未成功`,
              onOk() {
                that.goback();
              },
              onCancel() {
                that.Faildata = res.data.importFailList;
                that.dialogFailVisible = true;
              }
            });
          }
        }
      } else if (res.data === null) {
        this.$confirm({
          okText: "确定",
          title: res.msg
        });
      } else {
        this.$notification["error"]({
          message: "error",
          description: res.msg || "上传失败"
        });
        setTimeout(() => {
          this.reset();
        }, 2000);
      }
    },

    buCha() {
      const token = this.$store.state.auth.token;
      axios({
        url: "/hr/api/v1/empSocBackRecordRecords/exportErrorList", // 后端提供的导出接口
        data: this.errData, // 传递后端需要的导出参数
        method: "post",
        responseType: "blob",
        headers: {
          token: token,
          "Content-Type": "application/json"
        }
      })
        .then((response) => {
          const data = response.data;
          const url = URL.createObjectURL(data);
          const link = document.createElement("a");
          link.href = url;
          link.download = "异常数据表.xlsx"; // 自定义导出文件名
          link.click();
        })
        .catch((err) => {
          console.log("debug log --> ", err);
        });
    },
    handleUploadError() {
      this.$message.warning("没有文件");
    },
    handleFailClose() {
      this.dialogFailVisible = false;
      this.uploading = false;
      this.goback();
    },

    handleTableChange(pagination, filters, sorter) {
      this.pagination.current = pagination.current;
    },
    showFaild(numAll, numSuc, numFail, Faildata, downloadurl, columns) {
      this.columnDetail = [
        {
          title: "姓名",
          dataIndex: "name",
          scopedSlots: { customRender: "name" },
          ellipsis: true,
          width: 260
        },
        {
          title: "身份证号",
          dataIndex: "idCard",
          scopedSlots: { customRender: "idCard" },
          ellipsis: true,
          width: 260
        },
        {
          title: "回款途径",
          dataIndex: "wayStatus",
          scopedSlots: { customRender: "wayStatus" },
          ellipsis: true,
          width: 260
        },
        {
          title: "回款金额",
          dataIndex: "amount",
          scopedSlots: { customRender: "amount" },
          ellipsis: true,
          width: 260
        }
        // {
        //   title: "错误信息",
        //   dataIndex: "reason",
        //   fixed: "left",
        //   scopedSlots: { customRender: "reason" },
        //   ellipsis: true,
        //   width: 260
        // }
      ];
      this.count = numAll;
      this.succ = numSuc;
      this.fial = numFail;
      this.dataDetail = Faildata;
      if (this.count === this.succ) {
        this.dialogFailVisible = true;
      } else {
        this.callback = true;
      }
      // this.downloadurl = downloadurl;
      // columns.forEach((item, index) => {
      //   this.columnDetail.push({
      //     title: `${item}`,
      //     dataIndex: `${item}`,
      //     align: "center",
      //     width: item === "身份证号" ? 220 : 150,
      //     scopedSlots: { customRender: `${item}` },
      //     ellipsis: true
      //     // fixed: item === "身份证号" || item === "姓名" ? "left" : null
      //   });
      // });
    }
  }
};
</script>
<style lang="scss" scoped>
.header-title-box {
  padding-left: 24px;
  padding-right: 24px;
  margin-top: 10px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .headerTitle {
    display: flex;
    justify-content: flex-start;
    .block {
      width: 3px;
      height: 20px;
      margin-top: 2px;
      background-color: var(--primary);
      margin-right: 12px;
    }
  }
}
.uplaodBoxB {
  padding-bottom: 24px;
  .head {
    background-color: #fff;
    .anticon-left {
      z-index: 3;
      left: 24px;
      position: absolute;
      color: #595959;
      font-size: 20px;
      margin-top: 21px;
      color: #c0c0c0;
    }
    .left2 {
      z-index: 3;
      left: 45px;
      position: absolute;
      font-size: 14px;
      cursor: pointer;
      color: #c0c0c0;
    }
    font-size: 16px;
    color: #000;
    font-weight: 500;
    text-align: center;
    line-height: 60px;
    height: 60px;
    border-bottom: 1px solid #e8e8e8;
  }
  .exportTitle {
    text-align: center;
    font-size: 16px;
    font-weight: 500;
    color: #252525;
  }
  .left2 {
    z-index: 3;
    left: 50px;
    position: absolute;
    color: #595959;
    font-size: 14px;
  }
  .anticon-left {
    z-index: 3;
    left: 24px;
    position: absolute;
    color: #d8d8d8;
    font-size: 26px;
    margin-top: 17px;
  }
  .tipBox {
    line-height: 18px;
    background: #faf9f9;
    border-radius: 4px;
    font-size: 12px;
    color: #595959;
    margin: 0 24px;
    padding: 12px;
    // height: 170px;
  }
  .button {
    color: var(--primary);
    cursor: pointer;
  }
  .rowMargin {
    margin-top: 4px;
  }
  .ant-upload-select-text-box {
    border: 1px dashed #b8b8b8;
    background: #fff;
    margin: 24px;
    text-align: center;
    cursor: pointer;
    border-radius: 4px;
    padding: 12px;
  }
  .ant-upload-select-text-box ::v-deep .ant-upload-list {
    display: none;
  }
  .ant-upload-list-item-name {
    text-align-last: left;
  }

  .anticon-upload {
    font-size: 40px;
  }

  .ant-modal-body {
    line-height: 20px;

    em {
      color: var(--primary);
      font-style: normal;
    }
  }
}
.redc {
  color: red;
}
</style>
